package at.chl.genetic.util;

import java.util.ArrayList;
import java.util.List;

import at.chl.genetic.node.IDecisionNode;

public class NodeUtil {
	
	private static Long checkChildDepth=0l;

	public static List<IDecisionNode> getRootHeadedClonedDecisionTreeEntries(IDecisionNode root){
		
		List<IDecisionNode> entries=new ArrayList<IDecisionNode>();
		
		
		IDecisionNode clone=cloneNode(root);
		entries.add(clone);
		
		
		if((clone.getChildNodes()!=null)&&(clone.getChildNodes().size()>0)){
			
			fetchChilds(clone, entries);
		}
		
		if(entries.size()<=0){
			System.out.println();
		}
		
		return entries;
	}
	
	private static IDecisionNode cloneNode(IDecisionNode root) {
		
		IDecisionNode clone=root.cloneNode();
		
		return clone;
	}

	private static void fetchChilds(IDecisionNode node,List<IDecisionNode> entries){
		for(IDecisionNode childNode:node.getChildNodes()){
			if((childNode.getChildNodes()!=null)&&(childNode.getChildNodes().size()>0)){
				entries.add(childNode);
				fetchChilds(childNode, entries);
			}
		}
	}

	public static Long getNodeDepth(IDecisionNode subject,IDecisionNode root) {
		
		
		checkChildDepth=1l;
		
		Long depth=checkChilds(subject,root);
		
		checkChildDepth=1l;
		
		return depth;
	}

	private static Long checkChilds(IDecisionNode target, IDecisionNode node) {
		
		
		
		if(node.getChildNodes()!=null&&node.getChildNodes().size()>0){
			
			//count up to current depth
			checkChildDepth++;
			
			for(IDecisionNode child:node.getChildNodes()){
				if(child.equals(target)){
					//return the current depth
					return checkChildDepth;
				}else{
					//call the method again with the child
					checkChilds(target, child);
				}
			}
			
			//step down to previous depth
			checkChildDepth--;
		}
		return checkChildDepth;
	}
}
